package com.clstu.reflection;

import org.testng.annotations.Test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * 测试反射class得到类的结构信息
 */
public class ReflectionUtils {
    public static void main(String[] args) throws ClassNotFoundException {
        //得到class对象
        Class<?> personCls = Class.forName("com.clstu.reflection.Person");
        System.out.println(personCls.getName());//获取全类名
        System.out.println(personCls.getSimpleName());//获取简单类名
        Field[] fields = personCls.getFields();//获取全部public属性,包括父类的
        for (Field field : fields) {
            System.out.println("本类及其父类的属性======"+field);//
        }
        Field[] declaredFields = personCls.getDeclaredFields();//获取本类的所有属性
        for (Field declaredField : declaredFields) {
            System.out.println("奔雷所有属性=="+declaredField);
        }
        Method[] methods = personCls.getMethods();//获取所有public的方法,包括父类的(Object的方法也有(超类))(不包括构造器)
        for (Method method : methods) {
            System.out.println("本类的public方法"+method.getName());
        }
        Method[] declaredMethods = personCls.getDeclaredMethods();//获取所有的方法(本类)
        for (Method declaredMethod : declaredMethods) {
            System.out.println("本类的所有方法"+declaredMethod.getName());
        }
        Constructor<?>[] constructors = personCls.getConstructors();//获取所有的public构造器(不包括父类)
        for (Constructor<?> constructor : constructors) {
            System.out.println("所有public构造器"+constructor.getName());
        }
        Constructor<?>[] declaredConstructors = personCls.getDeclaredConstructors();//获取所有的构造器
        for (Constructor<?> declaredConstructor : declaredConstructors) {
            System.out.println("本类所有构造器"+declaredConstructor.getName());
        }
        System.out.println("父类"+personCls.getSuperclass());//得到父类
        Class<?>[] interfaces = personCls.getInterfaces();
        for (Class<?> anInterface : interfaces) {
            System.out.println("实现接口"+anInterface);
        }
        System.out.println("包信息"+personCls.getPackage());//得到包信息
        Annotation[] annotations = personCls.getAnnotations();//得到注解信息
        for (Annotation annotation : annotations) {
            System.out.println("注解信息"+annotation);
        }

    }

    @Test
    public void api() throws ClassNotFoundException {
        Class<?> personCls = Class.forName("com.clstu.reflection.Person");
        Field[] declaredFields = personCls.getDeclaredFields();//获取本类的所有属性
        for (Field declaredField : declaredFields) {
            System.out.println("本类所有属性=="+declaredField+ "改属性的修饰符:"+declaredField.getModifiers()+"该属性的类型"+declaredField.getType());
        }
        Method[] declaredMethods = personCls.getDeclaredMethods();//获取所有的方法(本类)
        for (Method declaredMethod : declaredMethods) {
            System.out.println("==========");
            System.out.println("本类的所有方法"+declaredMethod.getName()+"方法的修饰符"+declaredMethod.getModifiers()+"方法的返回类型"+declaredMethod.getReturnType());
            Class<?>[] parameterTypes = declaredMethod.getParameterTypes();
            for (Class<?> parameterType : parameterTypes) {
                System.out.println("形参类型:"+parameterType);
            }
        }
        Constructor<?>[] declaredConstructors = personCls.getDeclaredConstructors();//获取所有的构造器
        for (Constructor<?> declaredConstructor : declaredConstructors) {
            System.out.println("===========");
            System.out.println("本类所有构造器"+declaredConstructor.getName());
            Class<?>[] parameterTypes = declaredConstructor.getParameterTypes();
            for (Class<?> parameterType : parameterTypes) {
                System.out.println("改构造器的形参类型"+parameterType);
            }
        }

    }
}
class A{
    public A(String s){}
    public A(){}
    public int id;
    private String job;
}
interface IA{}
interface IB{}
@Deprecated
class Person extends A implements IA,IB{
    //字段
    private static String name;
    public  final int age= 20;
    int sal;
    protected Double weight;
    //构造器
    public Person(){}
    public Person(String s){}
    private Person(boolean b){}
    //方法
    public void f1(String name,int age ,boolean x){}
    private void  f2(){}
    void  f3(Double money,long x){}
    protected void f4(){};
}